
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "RawsPage.h"
#include "RawsDlg.h"

#include "../client/SettingsManager.h"

PropPage::TextItem RawsPage::texts[] = {
	{ IDC_ADD, ResourceManager::ADD },
	{ IDC_CHANGE, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE, ResourceManager::REMOVE },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item RawsPage::items[] = {
	{ 0, 0, PropPage::T_END }
};

LRESULT RawsPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items);

	CRect rc;

	ctrlRaws.Attach(GetDlgItem(IDC_LIST));
	ctrlRaws.GetClientRect(rc);

	ctrlRaws.InsertColumn(0, CTSTRING(SETTINGS_NAME), LVCFMT_LEFT, rc.Width(), 0);

	ctrlRaws.SetExtendedListViewStyle(BOOLSETTING(FULL_ROW_SELECT) ? LVS_EX_FULLROWSELECT : 0);

	// Do specialized reading here
	RawProfile::List lst = ClientProfileManager::getInstance()->getRawProfiles();

	for(RawProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
		RawProfile& cp = *i;	
		addEntry(cp, ctrlRaws.GetItemCount());
	}
	//Sulan start svn225
	int a = IDC_RAW_NAME1-1;			
	for(int r=1;r<11;++r){
		SetDlgItemText(++a, RAWSETTING(r).c_str());
	}
	//End
	return TRUE;
}

LRESULT RawsPage::onAddRaws(WORD , WORD , HWND , BOOL& ) {
	RawProfileDlg dlg;

	dlg.currentProfileId = -1;
	dlg.itemCount = ctrlRaws.GetItemCount();

	if(dlg.DoModal() == IDOK) {
		StringList sl;
		sl.push_back(dlg.name);
		sl.push_back(dlg.raw1);
		sl.push_back(dlg.raw2);
		sl.push_back(dlg.raw3);
		sl.push_back(dlg.raw4);
		sl.push_back(dlg.raw5);
		sl.push_back(dlg.raw6);
		sl.push_back(dlg.raw7);
		sl.push_back(dlg.raw8);
		sl.push_back(dlg.raw9);
		sl.push_back(dlg.raw10);
		addEntry(ClientProfileManager::getInstance()->addRawProfile(sl), ctrlRaws.GetItemCount());
	}
	return 0;
}

LRESULT RawsPage::onChangeRaws(WORD , WORD , HWND , BOOL& ) {
	if(ctrlRaws.GetSelectedCount() == 1) {
		int sel = ctrlRaws.GetSelectedIndex();
		
		RawProfileDlg dlg;
		dlg.currentProfileId = ctrlRaws.GetItemData(sel);
		dlg.itemCount = ctrlRaws.GetItemCount();

		if(dlg.DoModal() == IDOK) {
			ctrlRaws.SetItemText(dlg.currentProfileId, 0, dlg.name.c_str());
			dlg.currentRawProfile.setName(dlg.name);
			dlg.currentRawProfile.setRaw1(dlg.raw1);
			dlg.currentRawProfile.setRaw2(dlg.raw2);
			dlg.currentRawProfile.setRaw3(dlg.raw3);
			dlg.currentRawProfile.setRaw4(dlg.raw4);
			dlg.currentRawProfile.setRaw5(dlg.raw5);
			dlg.currentRawProfile.setRaw6(dlg.raw6);
			dlg.currentRawProfile.setRaw7(dlg.raw7);
			dlg.currentRawProfile.setRaw8(dlg.raw8);
			dlg.currentRawProfile.setRaw9(dlg.raw9);
			dlg.currentRawProfile.setRaw10(dlg.raw10);
			ClientProfileManager::getInstance()->updateRawProfile(dlg.currentRawProfile);
		}

		ctrlRaws.SetRedraw(FALSE);
		ctrlRaws.DeleteAllItems();
		RawProfile::List lst = ClientProfileManager::getInstance()->getRawProfiles();
		for(RawProfile::Iter j = lst.begin(); j != lst.end(); ++j) {
			RawProfile& cp = *j;	
			addEntry(cp, ctrlRaws.GetItemCount());
		}
		ctrlRaws.SelectItem(sel);
		ctrlRaws.SetRedraw(TRUE);
	}
	return 0;
}

LRESULT RawsPage::onRemoveRaws(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	if(ctrlRaws.GetSelectedCount() == 1) {
		int i = ctrlRaws.GetNextItem(-1, LVNI_SELECTED);
		ClientProfileManager::getInstance()->removeRawProfile(ctrlRaws.GetItemData(i));
		ctrlRaws.DeleteItem(i);
	}
	return 0;
}

void RawsPage::addEntry(const RawProfile& cp, int pos) {
	StringList lst;

	lst.push_back(cp.getName());
	ctrlRaws.insert(pos, lst, 0, (LPARAM)cp.getId());
}

void RawsPage::write() {
	ClientProfileManager::getInstance()->saveRawProfiles();
	//Sulan start svn225
	TCHAR buf[512];
	int a = IDC_RAW_NAME1-1;
	for(int r=1;r<11;++r){
		GetDlgItemText(++a, buf, 512);
		ClientProfileManager::getInstance()->setRawName(r, (string)buf);
	}
	ClientProfileManager::getInstance()->saveRawProfiles();
	//End
	PropPage::write((HWND)*this, items);
}
/**
 * @file
 */